package com.cloudcc.mobile.adapter;

import java.lang.reflect.Field;
import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.cloudcc.mobile.R;
import com.cloudcc.mobile.entity.Module;
import com.cloudcc.mobile.entity.Tabs;
import com.cloudcc.mobile.util.Tools;

/**
 * 首页菜单适配器
 * 
 * @author Administrator
 * 
 */
public class MenuNewAdapter extends BaseExpandableListAdapter {
	private Context context;
	private List<Module> list;

	public MenuNewAdapter(Context context, List<Module> list) {
		super();
		this.context = context;
		this.list = list;
	}

	public void setData(List<Module> list) {
		this.list = list;
	}

	@Override
	public int getGroupCount() {
		return list.size();
	}

	@Override
	public int getChildrenCount(int groupPosition) {
		return list.get(groupPosition).getTabs().size();
	}

	@Override
	public Module getGroup(int groupPosition) {
		return list.get(groupPosition);
	}

	@Override
	public Tabs getChild(int groupPosition, int childPosition) {
		return list.get(groupPosition).getTabs().get(childPosition);
	}

	@Override
	public long getChildId(int groupPosition, int childPosition) {
		return childPosition;
	}

	@Override
	public long getGroupId(int groupPosition) {
		return groupPosition;
	}

	@Override
	public boolean hasStableIds() {
		return false;
	}

	@Override
	public boolean isChildSelectable(int groupPosition, int childPosition) {
		return true;
	}

	@Override
	public View getGroupView(int groupPosition, boolean isExpanded,
			View convertView, ViewGroup parent) {
		GroupViewHolder holder = null;
		if (convertView == null) {
			convertView = LayoutInflater.from(context).inflate(
					R.layout.group_item_layout2, null);
			holder = new GroupViewHolder();
			holder.mGroupName = (TextView) convertView
					.findViewById(R.id.group_name);
			holder.imageview = (ImageView) convertView
					.findViewById(R.id.group_icon);
			convertView.setTag(holder);
		} else {
			holder = (GroupViewHolder) convertView.getTag();
		}
		holder.mGroupName.setText(list.get(groupPosition).getLabel());
		if (list.get(groupPosition) != null) {
			holder.imageview.setImageResource(R.drawable.drawable_expand_close);
		} else {
			holder.imageview.setImageResource(0);
		}
		// 点击展开
		if (isExpanded && list.get(groupPosition) != null) {
			holder.imageview.setImageResource(R.drawable.drawable_expand_open);
		}
		return convertView;
	}

	@Override
	public View getChildView(int groupPosition, int childPosition,
			boolean isLastChild, View convertView, ViewGroup parent) {
		ChildViewHolder holder = null;
		try {
			if (convertView == null) {
				convertView = LayoutInflater.from(context).inflate(
						R.layout.child_item_layout, null);
				holder = new ChildViewHolder();
				holder.mChildName = (TextView) convertView
						.findViewById(R.id.child_name);
				holder.mIcon = (ImageView) convertView
						.findViewById(R.id.child_icon);
				convertView.setTag(holder);
			} else {
				holder = (ChildViewHolder) convertView.getTag();
			}
			holder.mChildName.setText(getChild(groupPosition, childPosition)
					.getTabName());
			@SuppressWarnings("rawtypes")
			Class drawable = R.drawable.class;
			Field field = drawable.getField(getChild(groupPosition,
					childPosition).getImageId());
			int id = field.getInt(field.getName());
			holder.mIcon.setImageBitmap(null);
			holder.mIcon.setBackgroundResource(id);
		} catch (IllegalArgumentException e) {
			Tools.handle(e);
		} catch (NoSuchFieldException e) {
			Tools.handle(e);
		} catch (IllegalAccessException e) {
			Tools.handle(e);
		}

		return convertView;
	}

	class GroupViewHolder {
		TextView mGroupName;
		ImageView imageview;
	}

	class ChildViewHolder {
		ImageView mIcon;
		TextView mChildName;
	}
}
